package cn.lvji.springboot.learn3.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;

/**
 * 文件上传
 *
 * @author zhouzx
 */
@Slf4j
@Controller
public class MultipartFileController {

    @PostMapping("/file")
    @ResponseBody
    public String upload(String name, MultipartFile file) throws IOException {
        if (file.isEmpty()) {
            return "failed";
        }
        String fileName = file.getOriginalFilename();
        InputStream inputStream = file.getInputStream();

        log.info("file name is {}",fileName);
        return "success";
    }

    @PostMapping("/files")
    @ResponseBody
    public String uploads(String name, MultipartFile[] file) throws IOException {
        if (file.length==0) {
            return "failed";
        }

        log.info("{} file uploading",file.length);
        return "success";
    }
}
